#!/usr/bin/env python
#-*- coding:utf8 -*-
import web
import hashlib
from app.models.model import Model
from app.models.quest import Quest
from app.models.site_node import SiteNode
from app.helpers.nodes_helper import Helper

class Node(Model):
    table   = 'nodes'
    Quest   = Quest()
    SiteNode= SiteNode()

    #创建节点
    def create(self,data = None):
        try:
            status = self.insert(
                    uid     = data['uid'],
                    level  = data['level'],
                    name    = data['name'],
                    host    = data['host'],
                    token   = data['token']
                    )
            if status:
                return self.Quest.create(status,4)
        except:
            return False

    #删除节点
    def destory(self,id):
        #1.删除节点所有关联网站
        #self.SiteNode.delete_by("nid='%s'" % id)
        #2.删除节点所有其他列队任务4,5
        self.Quest.delete_by("sid='%s' and (type=4 or type=5)" % id)
        #3.标记节点为删除状态status =3
        self.save(id,status=3)
        #4.添加节点删除任务type = 6
        return self.Quest.create(id,6)

    #更新节点状态
    def update_by_status(self,id):
        #1.更新节点状态
        if self.save(id,status = 0):
            #2.更新节点网站关联状态
            return self.SiteNode.save_by('nid = %s' % id, status = 0)
        return False
